package tms.view;

import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import tms.Helper;
import tms.model.Account;
import tms.model.Booking;
import tms.model.Flight;
import tms.service.ServiceHandler;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class HistoryView extends JPanel {

    private static final long serialVersionUID = 1L;

    private String[] columnNames = {"Origin", "Destination", "Flight Day",
            "Arrived Day", "Flight Number", "Distance(km)" };

    private JTable table;
    private String level;
    private double totalDistance;

    public HistoryView(final Account user) {

        assert (user != null);
        setLayout(null);
        JPanel pane = new JPanel();

        pane.setBounds(0, 0, 600, 600);
        pane.setBorder(new EmptyBorder(5, 5, 5, 5));
        pane.setLayout(null);

        table = loadData(user.getId());

        Tools.addJLabel(pane, "Contact Information", Tools.TAHOMA_BOLD_14, 10, 11, 293, 14);
        Tools.addJLabel(pane, "Name:", null, 20, 36, 79, 14);
        Tools.addJLabel(pane, user.getFullName(), null, 103, 36, 167, 14);
        Tools.addJLabel(pane, "Address:", null, 20, 60, 73, 14);
        Tools.addJLabel(pane, user.getAddress(), null, 103, 60, 167, 14);
        Tools.addJLabel(pane, "Email:", null, 20, 85, 73, 14);
        Tools.addJLabel(pane, user.getEmail(), null, 103, 85, 167, 14);
        Tools.addJLabel(pane, "Mobile Number:", null, 20, 110, 98, 14);
        Tools.addJLabel(pane, user.getPhone(), null, 127, 110, 143, 14);
        Tools.addJLabel(pane, "Total Flight Distances:", null, 337, 36, 133, 14);
        Tools.addJLabel(pane, Double.toString(totalDistance), null, 474, 36, 69, 14);
        Tools.addJLabel(pane, "Km", null, 553, 36, 31, 14);
        Tools.addJLabel(pane, "Member Level:", null, 337, 60, 120, 14);
        Tools.addJLabel(pane, level, null, 467, 60, 76, 14);
        Tools.addJLabel(pane, "Detail Flight History", Tools.TAHOMA_BOLD_14, 10, 184, 152, 14);
        Tools.addJButton(pane, "Edit", null, 30, 138, 69, 23, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new RegisterView(user, true).setVisible(true);
            }
        }, null);
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setBounds(20, 210, 540, 180);
        pane.add(scrollPane);

        add(pane);
    }

    private JTable loadData(int userId) {

        //get all booked flight of current user by user's ID
        List<Booking> lsBooking = ServiceHandler.getListBooking("IDBookingAccount = '" + userId + "'");
        List<Flight> lsFlight = new ArrayList<Flight>();
        for (int i = 0; i < lsBooking.size(); i++) {
            Flight fl = ServiceHandler.getFirstFlight("Id = '" + lsBooking.get(i).getIdFlight() + "'");
            lsFlight.add(fl);
            totalDistance += fl.getDistance();
        }

        Object[][] data = new Object[lsFlight.size()][columnNames.length];
        for (int i = 0; i < lsFlight.size(); i++) {
                data[i][0] = lsFlight.get(i).getDeparting().getDestName();
                data[i][1] = lsFlight.get(i).getArriving().getDestName();
                data[i][2] = Helper.dateToString(lsFlight.get(i).getDepartureDate(), Tools.DATE_FORMAT);
                data[i][3] = Helper.dateToString(lsFlight.get(i).getArrivingDate(), Tools.DATE_FORMAT);
                data[i][4] = lsFlight.get(i).getFlightName();
                data[i][5] = lsFlight.get(i).getDistance();
        }

        if (totalDistance < 5000) {
            level = "Starter";
        } else if (totalDistance >= 5000 && totalDistance < 7000) {
            level = "Silver";
        } else if (totalDistance >= 7000 && totalDistance < 10000) {
            level = "Gold";
        } else {
            level = "Diamond";
        }

        JTable table = new JTable(data, columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);

        return table;
    }
}
